package com.drdj.exam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActivityAndData extends Activity {

	private final int REQUEST_CODE = 1;

	private EditText mEtStringData;
	private Button mBtnNewActivity;
	private Button mBtnNewActivityResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_and_data);

		mEtStringData = (EditText) findViewById(R.id.etStringData);
		mBtnNewActivity = (Button) findViewById(R.id.btnNewActivity);
		mBtnNewActivityResult = (Button) findViewById(R.id.btnNewActivityResult);

		/*
		 * start new activity with extra data.
		 */
		mBtnNewActivity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String textToSend = mEtStringData.getText().toString();
				Intent intent = new Intent(ActivityAndData.this, NewActivity.class);
				intent.putExtra("data", textToSend);
				startActivity(intent);
			}
		});

		/*
		 * start new activity with extra data. and receive result from the activity.
		 * 
		 * when the activity is closed, onActivityResult(int, int, Intent) method(of this class) will be called.
		 */
		mBtnNewActivityResult.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String textToSend = mEtStringData.getText().toString();
				Intent intent = new Intent(ActivityAndData.this, NewActivityForResult.class);
				intent.putExtra("data", textToSend);
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			Toast.makeText(this, "result : " + data.getStringExtra("result_data"), Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(this, "doesn't get exact result!", Toast.LENGTH_LONG).show();
		}
	}
}
